﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Yss.KFlow.Model
{
    /// <summary>
    /// 工具箱
    /// </summary>
    
    //public delegate void FlowTypeDrawChange(Util.FlowTypeDraw dtd,int arrow);
    public partial class Flow_ToolBox :UserControl 
    {
        ///// <summary>
        ///// 
        ///// </summary>
        //public event FlowTypeDrawChange ChangeTypeDraw;
        List<string> lDllNames = new List<string>();
        Util.FlowTypes ftd = Util.FlowTypes.None;

        bool flag = false ;
        /// <summary>
        /// True表示更新了操作类型,false表示没有 
        /// </summary>
        public bool Flag
        {
            get { return flag; }
            set { flag = value; }
        }
        /// <summary>
        /// 绘画类型
        /// </summary>
        public Util.FlowTypes FlowDraw
        {
            get { return ftd; }
            set { ftd = value; }
        }
        /// <summary>
        /// 容器属性
        /// </summary>
        public DevComponents.DotNetBar.SideBar Flow_SideBar
        {
            get
            {
                return toolPanel;
            }
        }
        int arrow = 0;
        /// <summary>
        /// 钢笔箭头类型 0，表示直线,1表示射线,2表示双箭头
        /// </summary>
        public int Arrow
        {
            get { return arrow; }
            set { arrow = value; }
        }
        /// <summary>
        /// 工具箱构造函数
        /// </summary>
        public Flow_ToolBox()
        {
            InitializeComponent();
        }

        private void AddNewTool_MouseDown(object sender, MouseEventArgs e)
        {
            
         
        }
        private void Tool_MouseDown(object sender, MouseEventArgs e)
        {
            if (null != sender as BaseTool)
            {
                Data.DragData dd = new Data.DragData();
                dd.Name = ((DevComponents.DotNetBar.ButtonItem)sender).Text;
                dd.Sender = sender;
                dd.YssTag = sender;
                dd.Pic = ((DevComponents.DotNetBar.ButtonItem)sender).Image;
                DoDragDrop(dd, DragDropEffects.Copy);
            }
        }

        private void loneCapLine_Click(object sender, EventArgs e)
        {
            
            FlowDraw = Util.FlowTypes.Line;
            Arrow = 1;
             flag = true;
          
        }

        private void twoCapsLine_Click(object sender, EventArgs e)
        {

            FlowDraw = Util.FlowTypes.Line;
            Arrow = 2;
             flag = true;
            
        }

        private void Lines_Click(object sender, EventArgs e)
        {

            FlowDraw = Util.FlowTypes.Line;
            Arrow = 0;
             flag = true;
           
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            FlowDraw = Util.FlowTypes.Del;
             flag = true;
            
            
        }

        private void loneCapLine_MouseDown(object sender, MouseEventArgs e)
        {
            FlowDraw = Util.FlowTypes.Line;
            Arrow = 1;
             flag = true;
            
            

        }

        private void twoCapsLine_MouseDown(object sender, MouseEventArgs e)
        {
            FlowDraw = Util.FlowTypes.Line;
            Arrow = 2;
            flag = true;
            
        }

        /// <summary>
        /// 方法：Lines_MouseDown;
        /// 参数：sender;
        /// 参数：e;
        /// 返回类型：
        /// 说明：
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void Lines_MouseDown(object sender, MouseEventArgs e)
        {
            FlowDraw = Util.FlowTypes.Line;
            Arrow = 0;
            flag = true;
            
        }

        private void btnDel_MouseDown(object sender, MouseEventArgs e)
        {
            //FlowDraw = Util.FlowTypeDraw.Del;
            //flag = true;
            //if (ChangeTypeDraw != null)
            //{
            //    ChangeTypeDraw(FlowDraw, Arrow);
            //}
        }

        private void AddNewTool_Click(object sender, EventArgs e)
        {
            //FlowDraw = Util.FlowTypeDraw.Del;
            //flag = true;
            //if (ChangeTypeDraw != null)
            //{
            //    ChangeTypeDraw(FlowDraw, Arrow);
            //}
            //this.contextMenuStrip1.AutoClose = false;
            //this.contextMenuStrip1.Close();
            //this.Refresh();

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.dll|*.dll";
            ofd.Title = "打开指定的自定义工具箱dll";
            ofd.SupportMultiDottedExtensions = false;
            ofd.RestoreDirectory = true;
            ofd.Multiselect = false;
            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            System.Reflection.Assembly ass;

            ass = System.Reflection.Assembly.LoadFile(ofd.FileName);
            Type[] types = ass.GetTypes();
            bool falg = true;
            DevComponents.DotNetBar.BaseItem bi = null;
            foreach (Type t in types)
            {
                if (typeof(BaseTool) != t.BaseType)
                {
                    continue;
                }
                else
                {
                    if (falg)
                    {
                        if (lDllNames.Contains(ofd.FileName))
                        {
                            return;
                        }
                        else
                        {
                            lDllNames.Add(ofd.FileName);
                        }
                        falg = false;
                        bi = new DevComponents.DotNetBar.SideBarPanelItem();
                        bi.Text = ass.ManifestModule.Name;
                        toolPanel.Panels.Add(bi);
                        bi . Expanded = true;

                    }
                    if (bi != null)
                    {
                        DevComponents.DotNetBar.BaseItem bii = (DevComponents.DotNetBar.BaseItem)ass.CreateInstance(t.FullName); bii.Name = t.Name;
                        ((DevComponents.DotNetBar.ButtonItem)bii).ButtonStyle = DevComponents.DotNetBar.eButtonStyle.ImageAndText;
                        bii.Text = t.Name;
                        bii.MouseDown += new MouseEventHandler(Tool_MouseDown);
                        bi.SubItems.Add(bii);
                    }
                }
            }
            this.Refresh();
        }

        private void twoCapsLine_DoubleClick ( object sender , EventArgs e )
        {
            //DevComponents . DotNetBar . ButtonItem c = sender as DevComponents . DotNetBar . ButtonItem;
            //if ( c != null )
            //{
            //    DevComponents . DotNetBar . Controls . TextBoxX tbX=new DevComponents.DotNetBar.Controls.TextBoxX ();
            //    tbX . Size = c . Size;
            //    tbX . Text = c . Text;
            //    tbX . Parent = this;
            //    tbX . Location =  new Point ( 30 , 40 );
            //    tbX . Visible = true;
               

            //}
        }

       
       

       

       
      

        
      
    }
}
